package com.stadium.controller;

import java.util.List;
import java.util.UUID;

import com.stadium.common.BaseResponse;
import com.stadium.common.enums.ResultEnum;
import com.stadium.common.exception.RRException;
import com.stadium.common.utils.CommonUtils;
import com.stadium.dto.RTeamUserDto;
import com.stadium.service.RTeamUserSV;
import com.stadium.vo.RTeamUserVo;
import com.stadium.vo.searchVo.RTeamUserSearchVo;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 球队人员关系表
 *
 * @author wsl
 * @email
 * @date 2021-05-04 09:39:10
 */
@RestController
@RequestMapping("/api/rteamuser")
@Api(tags = "球队人员关系表")
public class RTeamUserCL{
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private RTeamUserSV rTeamUserSV;

    @ApiOperation(value = "球队下保存/修改球员")
    @PostMapping(value = "/v1/save")
    public BaseResponse save(@Validated RTeamUserVo rTeamUserVo) {
        try {
            if (rTeamUserVo.getId() == null ||rTeamUserVo.getId() == ""){
                rTeamUserSV.save(rTeamUserVo);
            }else{
                    rTeamUserSV.update(rTeamUserVo);
            }
            return BaseResponse.ok();

        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "球员申请")
    @PostMapping(value = "/v1/apply")
    @ApiImplicitParams({@ApiImplicitParam(name="teamId",value="teamId"),
            @ApiImplicitParam(name="userId",value="userId")})
    public BaseResponse apply(@RequestParam(name = "teamId")String teamId,
                              @RequestParam(name = "userId")String userId) {
        try {
            rTeamUserSV.apply(teamId,userId);
            return BaseResponse.ok();
        } catch (RRException e){
            logger.warn(e.getMsg());
            return BaseResponse.error(e.getMsg());
        }
        catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "球队邀请球员")
    @PostMapping(value = "/v1/teamApply")
    @ApiImplicitParams({@ApiImplicitParam(name="teamId",value="teamId"),
            @ApiImplicitParam(name="userId",value="userId：球队id")})
    public BaseResponse teamApply(@RequestParam(name = "teamId")String teamId,
                              @RequestParam(name = "userId")String userId) {
        try {
            rTeamUserSV.teamApply(teamId,userId);
            return BaseResponse.ok();
        } catch (RRException e){
            logger.warn(e.getMsg());
            return BaseResponse.error(e.getMsg());
        }
        catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/v1/delete")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id")})
    public BaseResponse delete(@RequestParam(name = "id", required = true)String id) {
        try {
            rTeamUserSV.delete(id);
            return BaseResponse.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "审核球员-接收邀请")
    @PostMapping(value = "/v1/audit")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id"),
            @ApiImplicitParam(name="auditStatus",value="auditStatus1：审核通过 0：审核未通过")})
    public BaseResponse audit(@RequestParam(name = "id", required = true)String id,
                              @RequestParam(name = "auditStatus",required = true)String auditStatus) {
        try {
            rTeamUserSV.audit(id,auditStatus);
            return BaseResponse.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }



    @ApiOperation(value = "审核通知列表")
    @GetMapping(value = "/v1/query")
    public BaseResponse<List<RTeamUserDto>> query(@Validated RTeamUserSearchVo searchVo) {
        try {
            if (StringUtils.isNotEmpty(searchVo.getSearchValue())) {
                searchVo.setSearchValue("%" + searchVo.getSearchValue() + "%");
            }
            List<RTeamUserDto> queryList = rTeamUserSV.query(searchVo);
            int total = rTeamUserSV.getAuditCount(searchVo);
            return BaseResponse.ok(queryList, total);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

}
